package com.atguigu.ssyx.acl.service.impl;

import com.atguigu.ssyx.acl.mapper.RoleMapper;
import com.atguigu.ssyx.acl.service.AdminRoleService;
import com.atguigu.ssyx.acl.service.RoleService;
import com.atguigu.ssyx.model.acl.AdminRole;
import com.atguigu.ssyx.model.acl.Role;
import com.atguigu.ssyx.vo.acl.RoleQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Resource
    private AdminRoleService adminRoleService;

    /**
     * 角色列表（条件查询）
     * @param page
     * @param roleQueryVo
     * @return
     */
    @Override
    public IPage<Role> selectRolePage(Page<Role> page, RoleQueryVo roleQueryVo) {
        String roleName = roleQueryVo.getRoleName();
        // mp条件对象
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        // 判断是否为空，不为空则进行条件查询
        if (!StringUtils.isEmpty(roleName)) {
            wrapper.like(Role::getRoleName, roleName);
        }
        // 调用方法实现条件分页查询
        IPage<Role> rolePage = baseMapper.selectPage(page, wrapper);
        return rolePage;
    }

    /**
     * 取所有角色，根据id获取用户已分配角色列表
     * @param userId
     * @return
     */
    @Override
    public Map<String, Object> findRoleByUserId(Long userId) {
        // 获取所有角色
        List<Role> roleList = baseMapper.selectList(null);
        // 获取用户已分配角色列表
        // 根据id查询用户角色关系表
        LambdaQueryWrapper<AdminRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AdminRole::getAdminId, userId);
        List<AdminRole> adminRoleList = adminRoleService.list(wrapper);

        // 获取用户已分配角色id列表
        List<Long> roleIds = adminRoleList.stream()
                .map(AdminRole::getRoleId)
                .collect(Collectors.toList());

        // 存储用户配置角色列表
        List<Role> assignRoleList = new ArrayList<>();
        // 遍历所有角色列表，判断id是否包含在已分配角色列表中
        for (Role role : roleList) {
            if (roleIds.contains(role.getId())) {
                assignRoleList.add(role);
            }
        }

        Map<String, Object> roleMap = new HashMap<>();
        roleMap.put("allRolesList", roleList);
        roleMap.put("assignRoles", assignRoleList);
        return roleMap;
    }

    /**
     * 角色分配
     * @param adminId
     * @param roleId
     */
    @Override
    @Transactional
    public void saveAdminRole(Long adminId, List<Long> roleId) {
        //  删除用户角色关系
        // 根据用户id删除admin_role关系表中数据
        LambdaQueryWrapper<AdminRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AdminRole::getAdminId, adminId);
        adminRoleService.remove(wrapper);
        // 遍历所有角色id，添加用户角色关系
        List<AdminRole> adminRoleList = new ArrayList<>();
        for (Long id : roleId) {
            AdminRole adminRole = new AdminRole();
            adminRole.setAdminId(adminId);
            adminRole.setRoleId(id);
            adminRoleList.add(adminRole);
        }
        adminRoleService.saveBatch(adminRoleList);
    }
}
